package cn.itcast.mq.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;

@Component
public class SpringRabbitListener {

     @RabbitListener(queues = "simple.queue")
     public void listenSimpleQueue(String msg) {
         System.out.println("消费者接收到simple.queue的消息：【" + msg + "】  时间:"+ LocalDateTime.now().toString());
         // 消息丢失
         System.out.println(1/0);
         System.out.println("执行完, 消息具体代码");
     }
     // 延迟交换机
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("delay.queue"),
            exchange = @Exchange(value = "delay.direct",delayed = "true"),
            key = "delay"
    ))
    public void listenDlQueue(String msg) {
        System.out.println("消费者接收到延迟队列的消息：【" + msg + "】  时间:"+ LocalDateTime.now().toString());

    }
   /* @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(@Payload String msg, Message message, Channel channel) {
        System.out.println("消费者接收到simple.queue的消息：【" + msg + "】");
        System.out.println(1 / 0);
        System.out.println("执行完, 消息具体代码");
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/
}
